﻿using System;
using System.Collections.Generic;
using System.Text;

namespace csharpcore7_ch3
{
    class GenericExample2
    {
        static void _Main()
        {
            Dictionary<int, string> myDic = new Dictionary<int, string>();

            var myDic1 = new Dictionary<int, string>();

            Type a1 = typeof(A<>); //Unbound type
            Type a2 = typeof(A<,>);
            Type a3 = typeof(A<int, int>);//使用typeof指定封闭类型
            Console.WriteLine(a1.FullName);
            Console.WriteLine(a2.FullName);
            Console.WriteLine(a3.FullName);

            //值类型
            int[] intarr = {1,2,3};
            A<int>.Zap<int>(intarr);
            for (int i = 0; i < intarr.Length; i++)
            {
                Console.WriteLine("intarr[{0}]={1} ",i,intarr[i]);
            }

            //引用类型
            string[] strarr = { "str3", "str2", "str3" };
            A<string>.Zap<string>(strarr);
            for (int i = 0; i < strarr.Length; i++)
            {
                Console.WriteLine("strarr[{0}]={1} ", i, strarr[i]);
            }

            A A1 = new A();
            A A2 = new A();
            A A3 = new A();

            //引用类型
            A[] Aarr = { A1, A2, A3 };
            A<A>.Zap<A>(Aarr);
            for (int i = 0; i < Aarr.Length; i++)
            {
                Console.WriteLine("Aarr[{0}]={1} ", i, Aarr[i]);
            }
        }
    }

    public struct Nullable<T>
    {
        public T Value { get; }  //属性Value使用T
    }
    /// <summary>
    /// 泛型方法有多个参数
    /// </summary>
    /// <typeparam name="TKey"></typeparam>
    /// <typeparam name="TValue"></typeparam>
    class Dictionary<TKey, TValue> {

    }

    class A { }
    class A<T> {

        /// <summary>
        /// default 关键字可用于获取泛型类型参数的默认值。值类型默认为0，引用类型值默认为null
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="array"></param>
        public static void Zap<T>(T[] array)
        {
            for (int i = 0; i < array.Length; i++)
                array[i] = default(T);
        }
    }
    class A<T1, T2> { }
    
    /// <summary>
    /// 开放类型
    /// </summary>
    /// <typeparam name="T"></typeparam>
    class B<T>{
        void X()
        {
            Type t = typeof(T); 
        } 
    }
}
